package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import com.common.SessionContext;
import com.common.SessionInfo;
import com.utils.ResourceUtil;

/**
 * 监听在线用户上线下线
 * 
 * 
 */
public class OnlineListener implements ServletContextListener,
		ServletContextAttributeListener, HttpSessionListener,
		HttpSessionAttributeListener, HttpSessionActivationListener,
		HttpSessionBindingListener, ServletRequestListener,
		ServletRequestAttributeListener {

	private static final Logger logger = Logger.getLogger(OnlineListener.class);

	// private static ApplicationContext ctx = null;

	public OnlineListener() {
	}

	public void requestDestroyed(ServletRequestEvent arg0) {
	}

	/**
	 * 向session里增加属性时调用(用户成功登陆后会调用)
	 */
	public void attributeAdded(HttpSessionBindingEvent evt) {
		String name = evt.getName();
		logger.debug("向session存入属性：" + name);
		// if (ResourceUtil.getSessionInfoName().equals(name)) {//
		// 如果存入的属性是sessionInfo的话
		// HttpSession session = evt.getSession();
		// SessionInfo sessionInfo = (SessionInfo) session.getAttribute(name);
		// if (sessionInfo != null) {
		// OnlineService IonlineService = (OnlineService)
		// ctx.getBean("onlineService");
		// IonlineService.saveOrUpdateTonlineByLoginNameAndIp(sessionInfo.getLoginName(),
		// sessionInfo.getIp());
		// }
		// }
	}

	/**
	 * 服务器初始化时调用
	 */
	public void contextInitialized(ServletContextEvent evt) {
		logger.debug("服务器启动");
		// ctx =
		// WebApplicationContextUtils.getWebApplicationContext(evt.getServletContext());
	}

	public void sessionDidActivate(HttpSessionEvent arg0) {
	}

	public void valueBound(HttpSessionBindingEvent arg0) {
	}

	public void attributeAdded(ServletContextAttributeEvent arg0) {
	}

	public void attributeRemoved(ServletContextAttributeEvent arg0) {
	}

	/**
	 * session销毁(用户退出系统时会调用)
	 */
	public void sessionDestroyed(HttpSessionEvent evt) {
		HttpSession session = evt.getSession();
		if (session != null) {
			logger.debug("session销毁：" + session.getId());
			SessionInfo sessionInfo = (SessionInfo) session
					.getAttribute(ResourceUtil.getSessionInfoName());
			if (sessionInfo != null) {
				SessionContext sc = SessionContext.getInstance();
				//判断是否是重复登录，如果是重复登录就用sessionId删除
				if (sc.getRepeatLogin()) {
					// 判断是否存在此用户
					if (sc.getAllSession().containsKey(
							session.getId())) {
						// 消除session
						sc.DelSession(session.getId());
					}
				} else {
					// 判断是否存在此用户
					if (sc.getAllSession().containsKey(
							sessionInfo.getUser().getId())) {
						// 消除session
						sc.DelSession(sessionInfo.getUser().getId());
					}
				}
			}
		}
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
	}

	public void attributeAdded(ServletRequestAttributeEvent evt) {
	}

	public void valueUnbound(HttpSessionBindingEvent arg0) {
	}

	public void sessionWillPassivate(HttpSessionEvent arg0) {
	}

	public void sessionCreated(HttpSessionEvent arg0) {
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
	}

	public void attributeReplaced(ServletContextAttributeEvent arg0) {
	}

	public void attributeRemoved(ServletRequestAttributeEvent arg0) {
	}

	public void contextDestroyed(ServletContextEvent evt) {
		logger.debug("服务器关闭");
	}

	public void attributeReplaced(ServletRequestAttributeEvent arg0) {
	}

	public void requestInitialized(ServletRequestEvent arg0) {
	}

}
